/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.muni.fi.diplomka.datalayer.dao.ShopOfferDao;
import cz.muni.fi.diplomka.datalayer.entity.ShopOfferEntity;
import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class ShopOfferDaoImpl implements ShopOfferDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public ShopOfferDaoImpl() {
    }

    @Override
    public void addShopOffer(ShopOfferEntity shopOfferEntity) {
        if (shopOfferEntity == null) {
            throw new IllegalArgumentException("ShopOfferEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO shop_offer"
                    + "(player_id, city_id, sold_resource, sold_resource_count, wanted_resource, wanted_resource_count) VALUES"
                    + "(?,?,?,?,?,?)";
            ps = con.prepareStatement(insert);

            ps.setLong(1, shopOfferEntity.getPlayerId());
            ps.setLong(2, shopOfferEntity.getCityId());
            ps.setString(3, shopOfferEntity.getSoldResource().toString());
            ps.setInt(4, shopOfferEntity.getSoldResourceCount());
            ps.setString(5, shopOfferEntity.getWantedResource().toString());
            ps.setInt(6, shopOfferEntity.getWantedResourceCount());

            ps.executeUpdate();

            rs = ps.getGeneratedKeys();
            rs.next();

        } catch (SQLException ex) {
            Logger.getLogger(ShopOfferDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editShopOffer(ShopOfferEntity shopOfferEntity) {
        if (shopOfferEntity == null) {
            throw new IllegalArgumentException("ShopOfferEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE shop_offer SET city_id=?, sold_resource=?, sold_resource_count=?, wanted_resource=?, wanted_resource_count=? WHERE id=?";

            ps = con.prepareStatement(update);

            ps.setLong(1, shopOfferEntity.getPlayerId());
            ps.setLong(2, shopOfferEntity.getCityId());
            ps.setString(3, shopOfferEntity.getSoldResource().toString());
            ps.setInt(4, shopOfferEntity.getSoldResourceCount());
            ps.setString(5, shopOfferEntity.getWantedResource().toString());
            ps.setInt(6, shopOfferEntity.getWantedResourceCount());
            ps.setLong(7, shopOfferEntity.getId());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ShopOfferDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void deleteShopOffer(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String delete = "DELETE FROM shop_offer WHERE id=?";

            ps = con.prepareStatement(delete);

            ps.setLong(1, id);

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ShopOfferDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public ShopOfferEntity getShopOfferById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }

        ShopOfferEntity shopOfferEntity = new ShopOfferEntity();

        try {
            con = DbConnector.getDBConnection();
            String getShopOfferById = "SELECT * FROM shop_offer WHERE id=?";

            ps = con.prepareStatement(getShopOfferById);
            ps.setLong(1, id);

            rs = ps.executeQuery();

            while (rs.next()) {
                shopOfferEntity.setId(rs.getLong("id"));
                shopOfferEntity.setPlayerId(rs.getLong("player_id"));
                shopOfferEntity.setCityId(rs.getLong("city_id"));
                shopOfferEntity.setSoldResource(ResourceType.valueOf(rs.getString("sold_resource")));
                shopOfferEntity.setSoldResourceCount(rs.getInt("sold_resource_count"));
                shopOfferEntity.setWantedResource(ResourceType.valueOf(rs.getString("wanted_resource")));
                shopOfferEntity.setWantedResourceCount(rs.getInt("wanted_resource_count"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ShopOfferDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return shopOfferEntity;
    }

    @Override
    public List<ShopOfferEntity> getAllShopOffersOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("PlayerId can not be null");
        }
        List<ShopOfferEntity> shopOfferEntityList = new ArrayList<ShopOfferEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllShopOfferOfPlayer = "SELECT * FROM shop_offer WHERE player_id=?";

            ps = con.prepareStatement(getAllShopOfferOfPlayer);
            ps.setLong(1, playerId);

            rs = ps.executeQuery();

            while (rs.next()) {
                ShopOfferEntity shopOfferEntity = new ShopOfferEntity();
                shopOfferEntity.setId(rs.getLong("id"));
                shopOfferEntity.setPlayerId(rs.getLong("player_id"));
                shopOfferEntity.setCityId(rs.getLong("city_id"));
                shopOfferEntity.setSoldResource(ResourceType.valueOf(rs.getString("sold_resource")));
                shopOfferEntity.setSoldResourceCount(rs.getInt("sold_resource_count"));
                shopOfferEntity.setWantedResource(ResourceType.valueOf(rs.getString("wanted_resource")));
                shopOfferEntity.setWantedResourceCount(rs.getInt("wanted_resource_count"));

                shopOfferEntityList.add(shopOfferEntity);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ShopOfferDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return shopOfferEntityList;
    }

    @Override
    public List<ShopOfferEntity> getAllShopOffersNotOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("PlayerId can not be null");
        }
        List<ShopOfferEntity> shopOfferEntityList = new ArrayList<ShopOfferEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllShopOffersNotOfPlayer = "SELECT * FROM shop_offer WHERE player_id!=?";

            ps = con.prepareStatement(getAllShopOffersNotOfPlayer);
            ps.setLong(1, playerId);

            rs = ps.executeQuery();

            while (rs.next()) {
                ShopOfferEntity shopOfferEntity = new ShopOfferEntity();
                shopOfferEntity.setId(rs.getLong("id"));
                shopOfferEntity.setPlayerId(rs.getLong("player_id"));
                shopOfferEntity.setCityId(rs.getLong("city_id"));
                shopOfferEntity.setSoldResource(ResourceType.valueOf(rs.getString("sold_resource")));
                shopOfferEntity.setSoldResourceCount(rs.getInt("sold_resource_count"));
                shopOfferEntity.setWantedResource(ResourceType.valueOf(rs.getString("wanted_resource")));
                shopOfferEntity.setWantedResourceCount(rs.getInt("wanted_resource_count"));

                shopOfferEntityList.add(shopOfferEntity);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ShopOfferDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return shopOfferEntityList;
    }
}
